/*!
 Temelia - Heap samples.

 Copyright (C) 2008 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/samples.h"
#include <temelia/heap.h>

static void sample1()
{
	int *x, N = 200, i;
	heap_t heap;

	PRINT("\n\n Sample 1 started \n\n");

	heap = heap_new(10);

	heap_set_capacity_increment(heap, 0);

	for (i = 0; i < N; i++)
	{
		x = int_new(rand() % 1000);
		heap_insert(heap, x, int_compare, NULL);

		if (temelia_errno == FULL)
		{
			free(x);
			temelia_errno = NOT_EXCEPTION;
		}
	}

	while (!heap_is_empty(heap))
	{
		x = heap_get_min_key(heap);
		PRINT("%d\n", *(int *) x);
		heap_remove_min_key(heap, int_compare, NULL);
		free(x);
	}

	heap_iterate(heap, int_delete, NULL);
	heap_delete(heap);

	PRINT("\n\n Sample 1 ended \n\n");
}

static void sample2()
{
	int **x;
	long int N = 200;
	heap_t heap;

	PRINT("\n\n Sample 2 started \n\n");

	x = int_array_new(N, 1000);
	heap = heap_create_heap((void **) x, N, int_compare, NULL);

	PRINT("Empty %d, Full %d\n", heap_is_empty(heap), heap_is_full(heap));
	PRINT("Size %d, Elements number %d\n", heap_get_size(heap),
			heap_get_capacity(heap));

	PRINT("Removing keys from heap ... \n");
	while (!heap_is_empty(heap))
	{
		PRINT("%d\n", *(int *) heap_get_min_key(heap));
		heap_remove_min_key(heap, int_compare, NULL);
	}

	PRINT("Empty %d, Full %d\n", heap_is_empty(heap), heap_is_full(heap));
	PRINT("Size %d, Max size %d\n", heap_get_size(heap),
			heap_get_capacity(heap));

	heap_delete(heap);
	int_array_delete(x, N);

	PRINT("\n\n Sample 2 ended \n\n");
}

void run_heap_samples()
{
	sample1();
	sample2();
}
